package com.wuyan.locationdog.ui.my

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.SuccessBean
import com.arcsoft.commonbase.ext.afterTextChange
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.isEmpty
import com.arcsoft.commonbase.ext.textStringTrim
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DeviceList3ShareAdapter
import com.wuyan.locationdog.adapter.SharePersonListAdapter
import com.wuyan.locationdog.databinding.MyShareAddPersonListActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.ui.device.AddSuccess2Activity
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MyShareAddPersonListActivity : BaseBindingActivity<MyShareAddPersonListActivityBinding>() {

    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { SharePersonListAdapter(R.layout.item_share_person_list, null) }
    private val mAdapterDevice by lazy { DeviceList3ShareAdapter(R.layout.item_device_select_list3, null) }

    override fun initData() {
//        val deviceId = intent.extras?.getString("deviceId")

        setTitleText("添加共享")


        binding.apply {
            recyclerViewDevice.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapterDevice.apply {
                    setNewInstance(DeviceHelp.deviceShareList)
                }
            }
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                mAdapter.showStatus = false
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无联系人", com.arcsoft.commonbase.R.mipmap.empty_person))

                    addChildClickViewIds(R.id.itemLayout)
                    setOnItemChildClickListener { adapter, view, position ->
                        data.find { it.isSelect }?.isSelect = false
                        data[position].isSelect = true
                        notifyDataSetChanged()
                        etPhone.setText(data[position].userPhone)
                    }
                }
            }

            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["target"] = etPhone.textStringTrim()
                mViewModel.shareUserInfo(map)


            }
            etPhone.afterTextChange {
                btnEnable(btnSure,etPhone)
                if (etPhone.isEmpty()) {
                    mAdapter.data.find { it.isSelect }?.isSelect = false
                    mAdapter.notifyDataSetChanged()
                }
            }
            btnEnable(btnSure,etPhone)
        }

        mViewModel.shareRecentPersonList(hashMapOf())
    }

    override fun observe() {
        super.observe()

        mViewModel.anyBean.observe(this) {
            DeviceHelp.successBean = SuccessBean("共享成功","请等待好友接受分享")
            ActivityUtils.startActivity(AddSuccess2Activity::class.java)
        }
        mViewModel.deviceShareBean.observe(this) {
            mAdapter.setNewInstance(it.content.toMutableList())

        }
        mViewModel.deviceShareUserInfoBean.observe(this) {
            CommonDialog(mActivity, title = "确定添加 [${it.content.nickName}] 的使用权限？", "") {
                val map = HashMap<String, Any>()
                map["deviceIds"] = DeviceHelp.deviceShareList.map { it.deviceId }
                map["target"] = binding.etPhone.textStringTrim()
                mViewModel.addShare(map)
            }.show()

        }

    }


}